//
//  CYPassWordCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/1.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYPassWordCell.h"
#import "CYTextField.h"
@interface CYPassWordCell ()<CYTextFieldDelegate>
@property (nonatomic, strong) CYTextField *textField;
@end
@implementation CYPassWordCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (CYTextField *)textField{
    if (!_textField) {
        _textField = [[CYTextField alloc] initWithPlaceholder:nil];
        _textField.cydelegate = self;
        _textField.keyboardType = UIKeyboardTypeEmailAddress;
        [self addSubview:_textField];
        
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH -  2 * 20, AUTO(60)));
        }];
    }
    return _textField;
}

- (void)setPlaceholder:(NSString *)placeholder{
    if (_placeholder != placeholder) {
        _placeholder = placeholder;
        self.textField.placeholder = placeholder;
        [self setupTextField];
    }
}

- (void)setText:(NSString *)text{
    if (_text != text) {
        _text = text;
        _textField.text = text;
        self.block(_textField.placeholder, _textField.text);
    }
}

- (void)setupTextField{
    if ([_placeholder isEqualToString:@"请输入账号"])
        _textField.enabled = NO;
    else if ([_placeholder isEqualToString:@"选择密保问题"]){
        _textField.rightView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xiala"]];
        _textField.rightViewMode = UITextFieldViewModeAlways;
    }
}


#pragma - mark - CYTextFieldDelegate - method
- (BOOL)textFieldShouldBeginEditing:(CYTextField *)textField{
    if ([textField.placeholder isEqualToString:@"选择密保问题"]) {
        textField.line.backgroundColor = [UIColor lightGrayColor];
        [textField.line mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
        }];
        _block(textField.placeholder,nil);//下拉选择
        return NO;
    }
    textField.secureTextEntry = [textField.placeholder containsString:@"密码"]?YES:NO;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.text = textField.text;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([textField.placeholder containsString:@"密码"]) {
        if ([textField.text includeChinese])
            textField.text = nil;
        else if (textField.text.length >= 6){
            textField.text = [textField.text substringToIndex:6];
            return NO;
        }
    }
    return YES;
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
